import pymysql
class Mysql():
    #传参
    def __init__(self,host,user,password,database,cursorclass=None):
        self.host=host
        self.user=user
        self.password=password
        self.database=database
        if cursorclass:
            self.cursorclass=cursorclass
        else:
            self.cursorclass=None

    def connect(self):
        self.db=pymysql.connect(self.host,self.user,self.password,self.database,cursorclass=self.cursorclass)
        self.cursor=self.db.cursor()

    def get_one(self,sql):
        result=0
        try:
            self.connect()
            self.cursor.execute(sql)
            result=self.cursor.fetchone
            self.close()
            return result
        except Exception as E:
            print('select error',E)
        return result
    def get_all(self,sql):
        result=0
        try:
            self.connect()
            self.cursor.execute(sql)
            result=self.cursor.fetchall()
            self.close()
        except Exception as E:
            print('select error:',E)
        return result

    def close(self):
        self.cursor.close()
        self.db.close()
